<template>
  <div>
    <ylheader @save="savecaogao" @send='sendcomment' :lefttext="true" :lefttexts="'取消'" :title="'发评论'" :righttext="true" :righttexts="'发送'" ></ylheader>
    <div>
    <div class="loadmore-container" ref="containerWrapper">
    <div class="loadmore-ul">
      <publishshuoshuo :textareatexts="content" :thumbnailslist="pics"  @haha="textareatext" @commentpics="uploadpics"></publishshuoshuo>
    </div>
    </div>
    </div>
  </div>
</template>
<script>
import ylheader from "src/components/yl-header.vue";
import publishshuoshuo from "src/components/addcomments.vue";
import api from "src/api/index";
export default {
  name:"publishreplacecomments",
  data() {
    return {
      content: "",
      pics: [],
      uploadpictures: [],
      picstring: [],
      caogaoId: "",
      cansend: true
    };
  },
  mounted() {
    this.initReplacecommentsdraft().then(data => {
      this.initScroll();
    });
  },
  components: {
    ylheader,
    publishshuoshuo
  },
  methods: {
    initReplacecommentsdraft() {
      let p = new Promise((resolve, reject) => {
        const data = {
          replacedId: this.$route.query.replacedId
        };
        /**
             * 查询评论草稿
             */
        api.Queryreplacecommentsdraft(data).then(res => {
          if (res.success) {
            if (res.data) {
              this.pics = res.data.picture;
              this.content = res.data.content;
              this.caogaoId = res.data.id;
            }
            resolve("获取评论草稿...");
          } else {
            resolve("获取草稿失败...");
          }
        });
      });
      return p;
    },
    //初始化scroll
    initScroll() {
      let p = new Promise((resolve, reject) => {
        this.$nextTick(() => {
          let containerWrapper = this.$refs.containerWrapper;
          this.setScroll(containerWrapper, this);
        });
        resolve("初始化scroll结束...");
      });
      return p;
    },
    setScroll(containerWrapper, _this) {
      _this.myscroll = new iScroll(containerWrapper, {
        //上拉时触发
        onScrollMove: function() {
          if (
            this.y <= this.wrapperH - this.scroller.clientHeight - 50 &&
            _this.is_r == false
          ) {
            //加载数据
            _this.is_r = true;
          }
        },
        onScrollEnd: function() {}
      });
    },
    textareatext(data) {
      this.content = data;
    },
    uploadpics(data) {
      this.uploadpictures = data;
    },
    savecaogao() {
      this.uploadpictures = [...this.pics];
      const array = [];
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src);
      });
      this.picstring = array.join(",");
      if (!this.content && !this.picstring) {
        this.$router.go(-1);
        return;
      }
      const replacedreply = {
        replacedUserId: this.$route.query.replacedUserId,
        content: this.content,
        draft: 1,
        pictureJson: this.picstring,
        replacedId: this.$route.query.replacedId,
        parentId: this.$route.query.parentId,
        replyUserId: this.$route.query.replyUserId,
        id: this.caogaoId
      };
      api.Queryreplacepublishcomments(replacedreply).then(res => {
        if (res.success) {
          this.$router.go(-1);
        } else {
          this.$toast(res.msg);
        }
      });
    },
    sendcomment() {
      if (this.cansend == false) {
        return;
      }
      
      this.uploadpictures = [...this.pics];
      const array = [];
      _each(this.uploadpictures.slice(0, 9), (v, i) => {
        array.push(v.src);
      });
      this.picstring = array.join(",");

      if (!this.content && !this.picstring) {
        this.$toast("请编写评论");
        return;
      }
      // this.content = this.$util.getTextUrl(this.content);//识别链接
      const replacedreply = {
        replacedUserId: this.$route.query.replacedUserId,
        content: this.content,
        draft: 0,
        pictureJson: this.picstring,
        replacedId: this.$route.query.replacedId,
        parentId: this.$route.query.parentId,
        replyUserId: this.$route.query.replyUserId,
        id: this.caogaoId
      };
      api.Queryreplacepublishcomments(replacedreply).then(res => {
        if (res.success) {
           this.$util.saveFootPrint({actType:6,typeValue:res.value});//用户轨迹
          //更新评论数据
          if (typeof(this.$route.query.replaceIndex)!= "undefined") {
            let placeObj = JSON.parse(localStorage.placeStorage);
            if(this.$route.query.replaceIndex<placeObj.commentlist.length){
              placeObj.commentlist[this.$route.query.replaceIndex]
                .commentNumber++;
            }
            let tempPlaceObj = JSON.stringify({
              commentlist: placeObj.commentlist,
              hasmore: placeObj.hasmore,
              totalPage: placeObj.totalPage,
              page: placeObj.page,
              selected: placeObj.selected,
              myid: placeObj.myid,
              pageone: placeObj.pageone,
              pagetwo: placeObj.pagetwo,
              pagethree: placeObj.pagethree
            });
            localStorage.placeStorage = tempPlaceObj;
          }
          this.cansend = true;
          this.$router.go(-1);
        } else {
          this.$toast(res.msg);
          this.cansend = true;
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.loadmore-container {
  height: 95vh;
  overflow: hidden;
  margin: 0 auto;
}
.loadmore-ul {
  padding-bottom: 0.2rem;
  list-style: none;
}
</style>